package com.shaoshaohuo.app.ui.ec;

import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ScrollView;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.db.DaoUtils;
import com.shaoshaohuo.app.entity.Address;
import com.shaoshaohuo.app.entity.AddressEntity;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.DefaultAddressEntity;
import com.shaoshaohuo.app.entity.EcCategoryThird;
import com.shaoshaohuo.app.entity.EcVarietyEntity;
import com.shaoshaohuo.app.entity.EditPurchasePublished;
import com.shaoshaohuo.app.entity.Market;
import com.shaoshaohuo.app.entity.PurchaseDetail;
import com.shaoshaohuo.app.entity.PurchaseDetailEntity;
import com.shaoshaohuo.app.entity.post.PurchasePublished;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.ui.AddressManagerActivity;
import com.shaoshaohuo.app.ui.BaseActivity;
import com.shaoshaohuo.app.utils.AreaUtil;
import com.shaoshaohuo.app.utils.CityinfoUtil;
import com.shaoshaohuo.app.utils.DeviceHelper;
import com.shaoshaohuo.app.utils.StringHelper;
import com.shaoshaohuo.app.utils.city.Cityinfo;
import com.shaoshaohuo.app.view.BuyProduct;
import com.shaoshaohuo.app.view.DatepickerPopwindow;
import com.shaoshaohuo.app.view.DatepickerPopwindow.OnSelectDateListener;
import com.shaoshaohuo.app.view.TimepickerPopwindow;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.view.citymenu.CascadingMenuPopWindow;
import com.shaoshaohuo.app.view.citymenu.entity.Area;
import com.shaoshaohuo.app.view.citymenu.interfaces.CascadingMenuViewOnSelectListener;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;

/**
 * @ClassName BuysGoodsActivity
 * @author linmu
 * @Decription 我要买货 @2015-8-30 下午5:48:00
 */
public class BuysGoodsActivity extends BaseActivity implements OnClickListener, OnCheckedChangeListener {

	private static final int SELECT_ADDRESS_POI = 1;
	private static final int SELECT_PRODUCT_TYPE = SELECT_ADDRESS_POI + 1;
	private static final int SELECT_MARKET_CITY = SELECT_PRODUCT_TYPE + 1;
	private static final int SELECT_MARKET = SELECT_MARKET_CITY + 1;

	private TopbarView mTopbarView;

	private View mStartTimeLayout;
	private TextView mStartTimeText;// 开始时间
	private View mEndTimeLayout;
	private TextView mEndTimeText;// 结束时间
	private View mRecivAddressLayout;
	private TextView mRecivAddressText;// 收获地址
	private EditText mBuyYaoqiuEdit;// 要求
	private View mPifashichangLayout;
	private TextView mPifashichangEdit;// 批发市场
	private EditText mDangkouweizhiEdit; // 档口位置
	private EditText mNiancaigoueEdit; // 年采购量
	private CascadingMenuPopWindow cascadingMenuPopWindow;
	private CascadingMenuPopWindow cascadingMenuPopWindowMarket;
	private Button mNextButton; // 发布

	private boolean isSend;

	private AlertDialog unitDialog;
	private DatepickerPopwindow datePop;
	private TimepickerPopwindow timePop;

	private String sendDate = ""; // 购买时间
	private String wantDate = ""; // 结束时间

	private Cityinfo marketCity; // 选中的市
	private Market selectMarket;

	private LinkedList<BuyProduct> mProductList = new LinkedList<>(); // 保存多个
	// 想要发布的商品
	// 最多五个
	private Address address;// 收获地址
	private LinearLayout mAddList;
	private LinearLayout mReleaseView;
	private int releaseHight;
	private CheckBox mIsRelease; // 是否发布到 商机
	private ScrollView scrollView;
	private TextView tv_release_to;

	private boolean isEditStatus = false;
	public static final String EXTRA_RECORD_ID = "EXTRA_RECORD_ID";
	private String recordIdEdit;
	private PurchaseDetailEntity supplyEntityEdit;//被编辑的对象

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new TopbarView(this));
		recordIdEdit = getIntent().getStringExtra(EXTRA_RECORD_ID);
		if (recordIdEdit != null) {
			isEditStatus = true;
		}
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				setContentView(R.layout.activity_buy_goods);
				initView();
				setUpView();
				if (isEditStatus) {
					initEditData();
				}
			}
		}, 200);
	}

	private void initView() {
		mTopbarView = (TopbarView) this.findViewById(R.id.topbar);
		addView(null);
		hideRelease();
		mIsRelease = (CheckBox) findViewById(R.id.ib_release_to);
		tv_release_to = (TextView) findViewById(R.id.tv_release_to);
		scrollView = (ScrollView) findViewById(R.id.scrollview);

		mStartTimeLayout = this.findViewById(R.id.layout_start_time);
		mStartTimeText = (TextView) this.findViewById(R.id.textview_start_time);
		mEndTimeLayout = this.findViewById(R.id.layout_end_time);
		mEndTimeText = (TextView) this.findViewById(R.id.textview_end_time);
		mRecivAddressLayout = this.findViewById(R.id.layout_recev_address);
		mRecivAddressText = (TextView) this.findViewById(R.id.textview_recev_address);

		mBuyYaoqiuEdit = (EditText) this.findViewById(R.id.edittext_buy_yaoqiu);
		mPifashichangLayout = this.findViewById(R.id.layout_pifashichang);

		mPifashichangEdit = (TextView) this.findViewById(R.id.textview_pifashichang);
		mDangkouweizhiEdit = (EditText) this.findViewById(R.id.textview_dangkouweizhi);
		mNiancaigoueEdit = (EditText) this.findViewById(R.id.edittext_niancaigoue);

		mNextButton = (Button) this.findViewById(R.id.button_next);

	}

	private void setUpView() {
		mTopbarView.setCenterText("我要买货");
		mTopbarView.setLeftView(true, true);
		mIsRelease.setOnCheckedChangeListener(this);
		tv_release_to.setOnClickListener(this);

		mStartTimeLayout.setOnClickListener(this);
		mEndTimeLayout.setOnClickListener(this);
		mRecivAddressLayout.setOnClickListener(this);
		mPifashichangLayout.setOnClickListener(this);
		mNextButton.setOnClickListener(this);

		// 获取默认收货地址
		getDefaultAddress();

	}

	private void initEditData() {
		if (isEditStatus) {
			mNextButton.setText("确认修改");
		}
		startLoadingDialog();
		RequestService.getInstance().getPurchaseDetail(this, recordIdEdit, PurchaseDetailEntity.class, new RequestListener() {


			public void onSuccess(int requestCode, BaseEntity resultData) {
				dismissLoadingDialog();
				if (resultData.isOk()) {
					supplyEntityEdit = (PurchaseDetailEntity) resultData;
					updateUi(supplyEntityEdit);
				} else {
					showToast(resultData.getMsg());
				}
			}

			@Override
			public void onFailed(int requestCode, Exception error, String msg) {
				dismissLoadingDialog();
				showToast(R.string.please_check_network);
			}
		});

	}

	@Override
	public void onActivityReenter(int resultCode, Intent data) {
		super.onActivityReenter(resultCode, data);
	}

	/**
	 * 编辑状态时，初始化数据
	 * @param entity
	 */
	private void updateUi(PurchaseDetailEntity entity) {
		final PurchaseDetail order = entity.getData();
		final BuyProduct product = mProductList.get(0);
		product.setUnit(order.getUnit());
		product.setEcCategorySecond(DaoUtils.findEcCategorySecond(order.getPid()));
		product.setmNumberView(order.getNum());
		product.setmGuigeView(order.specifications);
		RequestService.getInstance().getEcCategoryVariety(this, order.getBigpid(), order.getPid(),
				EcVarietyEntity.class, new RequestListener() {

					@Override
					public void onSuccess(int requestCode, BaseEntity resultData) {
						if (resultData.isOk()) {
							EcVarietyEntity entity = (EcVarietyEntity) resultData;
							ArrayList<EcCategoryThird> mSelectVarietyList = (ArrayList<EcCategoryThird>) entity.getData().getList();
							for (EcCategoryThird ec : mSelectVarietyList) {
								if (ec.getCatid() != null && ec.getCatid().equals(order.getCatid())) {
									product.setSelectVariety(ec);
								}
							}
						}
					}

					@Override
					public void onFailed(int requestCode, Exception error, String msg) {

					}
				});
		RequestService.getInstance().getOftenAddress(this, AddressEntity.class, new RequestListener() {
			@Override
			public void onSuccess(int requestCode, BaseEntity resultData) {
				if (resultData.isOk()) {
					AddressEntity addressEntity = (AddressEntity) resultData;
					if (addressEntity.getData() != null) {
						for (Address ad : addressEntity.getData()) {
							if (ad.getCityid() != null && ad.getCityid().equals(order.getCityid())) {
								address = ad;
								if (address != null) {
									mRecivAddressText.setText(AreaUtil.getAreaFullname(address.getCityid()) + address.getAddress());
								}
							}

						}

					}
				}
			}

			@Override
			public void onFailed(int requestCode, Exception error, String msg) {

			}
		});

		mBuyYaoqiuEdit.setText(order.getContent());
		if (!"0".equals(order.getStart())) {
			mStartTimeText.setText(StringHelper.formatDate(order.getStart()));
		}
		if (!"0".equals(order.getEnd())) {
			mEndTimeText.setText(StringHelper.formatDate(order.getEnd()));
		}

//		mIsRelease.setChecked(order.get);
	}

	/**
	 * 获取收获地址 填充
	 */
	private void getDefaultAddress() {
		RequestService.getInstance().getDefaultAddress(this, DefaultAddressEntity.class, new RequestListener() {
			@Override
			public void onSuccess(int requestCode, BaseEntity resultData) {
				if (resultData.isOk()) {
					DefaultAddressEntity addressEntity = (DefaultAddressEntity) resultData;
					address = addressEntity.getData();
					if (address != null) {
						mRecivAddressText.setText(address.getBpcname());
					}
				}
			}

			@Override
			public void onFailed(int requestCode, Exception error, String msg) {

			}
		});
	}

	@Override
	public void onClick(View v) {
		DeviceHelper.hideIME(mTopbarView);
		switch (v.getId()) {
			case R.id.layout_start_time:
				isSend = true;
				selectDate();
				break;
			case R.id.layout_end_time:
				isSend = false;
				selectDate();
				break;
			case R.id.layout_recev_address:
				Intent intent = new Intent(this, AddressManagerActivity.class);
				intent.putExtra(ExtraName.isForResult, true);
				startActivityForResult(intent, SELECT_ADDRESS_POI);

				break;
			case R.id.button_next:
				if (isEditStatus) {
					showSureDialogEdit();
				} else {
					showSureDialog();
				}
				break;
			case R.id.tv_release_to:
				if (!mIsRelease.isChecked()) {
					showRelease();
					mIsRelease.setChecked(true);
				} else {
					hideRelease();
					mIsRelease.setChecked(false);
				}
				break;
			case R.id.layout_pifashichang:
				selectMarketCity();
				break;
		}
	}

	private void showSureDialogEdit() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("确认修改?");
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				releaseEdit();
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
			}
		});
		builder.create().show();
	}

	private void releaseEdit() {

		String title = "";
		String bigpid = "";
		String pid = "";
		String catid = "";
		String varieties = "";
		String specifications = "";
		String num = "";
		String unit = "";
		boolean flag = false;
		for (BuyProduct buyProduct : mProductList) {
			if (buyProduct.check()) {
				flag = true;
				title += buyProduct.getSecond().getCatname() + ",";
				bigpid += buyProduct.getSecond().getBigpid() + ",";
				pid += buyProduct.getSecond().getCatid() + ",";
				catid += buyProduct.getThird().getCatid() + ",";
				varieties += buyProduct.getThird().getCatid() + ",";
				num += buyProduct.getNum() + ",";
				unit += buyProduct.getUnit() + ",";
				String guige = buyProduct.getGuige();
				specifications += guige + ",";
			}
		}
		if (!flag) {
			showToast("至少填写一个完整商品信息！");
			return;
		}

		title = title.substring(0, title.length() - 1);
		bigpid = bigpid.substring(0, bigpid.length() - 1);
		pid = pid.substring(0, pid.length() - 1);
		catid = catid.substring(0, catid.length() - 1);
		varieties = varieties.substring(0, varieties.length() - 1);
		num = num.substring(0, num.length() - 1);
		unit = unit.substring(0, unit.length() - 1);
		specifications = specifications.substring(0, specifications.length() - 1);
		if (address == null) {
			showToast("请选择收获地址");
			return;
		}
		EditPurchasePublished purchase = new EditPurchasePublished();
		purchase.setNumber(supplyEntityEdit.getData().getNumber());
//		PurchasePublished purchase = new PurchasePublished();
		purchase.setTitle(title);
		purchase.setBigpid(bigpid);
		purchase.setPid(pid);
		purchase.setCatid(catid);
		purchase.setVarieties(varieties);
		purchase.setSpecifications(specifications);
		purchase.setNum(num);
		purchase.setUnit(unit);
		purchase.setCityid(address.getCityid());
		purchase.setAddressId(address.getId());

		purchase.setContent(mBuyYaoqiuEdit.getText().toString().trim());
		purchase.setStart(sendDate);
		purchase.setEnd(wantDate);

		if (mIsRelease.isChecked()) {
			if (marketCity != null) {
				purchase.setMarketcityid(marketCity.getId());
				purchase.setMarketcitycode(marketCity.getId());
				purchase.setLng(marketCity.lng);
				purchase.setLat(marketCity.lat);
			}
			if (selectMarket != null) {
				purchase.setMarketid(selectMarket.getId());
			}
		}

		purchase.setPosition(mDangkouweizhiEdit.getText().toString().trim());
		purchase.setYearmoney(mNiancaigoueEdit.getText().toString().trim());

		startLoadingDialog();
		RequestService.getInstance().publishedPurchaseRecordEdit(this, purchase, BaseEntity.class, new RequestListener() {

			@Override
			public void onSuccess(int requestCode, BaseEntity resultData) {
				dismissLoadingDialog();
				if (resultData.isOk()) {
					showToast("修改成功");
					toPurchaseHall();
					finish();
				} else {
					showToast(resultData.getMsg());
				}
			}

			@Override
			public void onFailed(int requestCode, Exception error, String msg) {
				dismissLoadingDialog();
				showToast(R.string.please_check_network);
			}
		});
	}

	/**
	 * 选择批发市场城市
	 */
	private void selectMarketCity() {
		if (cascadingMenuPopWindowMarket == null) {
			// 向三级menu添加地区数据
			cascadingMenuPopWindowMarket = new CascadingMenuPopWindow(this, false);
			cascadingMenuPopWindowMarket.setMenuViewOnSelectListener(new CascadingMenuViewOnSelectListener() {

				@Override
				public void getValue(Area city, String fullname) {
					marketCity = CityinfoUtil.convert(city);
					selectMarket();
				}

			});
			cascadingMenuPopWindowMarket.showAsDropDown(mPifashichangEdit, 5, 5);
		} else if (cascadingMenuPopWindowMarket != null && cascadingMenuPopWindowMarket.isShowing()) {
			cascadingMenuPopWindowMarket.dismiss();
		} else {
			cascadingMenuPopWindowMarket.showAsDropDown(mPifashichangEdit, 5, 5);
		}
	}

	/**
	 * 选择批发市场
	 */
	private void selectMarket() {
		Intent intent = new Intent(this, SelectMarketActivity.class);
		intent.putExtra(ExtraName.cityid, marketCity.getId());
		startActivityForResult(intent, SELECT_MARKET);
	}

	/**
	 * 选择时间
	 */
	private void selectDate() {
		if (datePop != null && datePop.isShowing()) {
			return;
		}
		if (datePop == null) {

			datePop = new DatepickerPopwindow(this);
			datePop.setOnSelectDateListener(new OnSelectDateListener() {

				@Override
				public void onSelectDate(String date) {
					if (isSend) {
						sendDate = date;
						mStartTimeText.setText(sendDate);
					} else {
						// wantTimeYear = year;
						// wantTimeMonth = monthOfYear;
						// wantTimeDay = dayOfMonth;
						wantDate = date;
						mEndTimeText.setText(wantDate);
					}
				}
			});
		}
		datePop.showLocation(mTopbarView);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK || resultCode == SelectSpecificationsActivity.SELECT_OK) {
			switch (requestCode) {
				case SELECT_ADDRESS_POI:
					Serializable serializableExtra = data.getSerializableExtra(ExtraName.address);
					if (serializableExtra != null) {
						address = (Address) serializableExtra;
						mRecivAddressText.setText(AreaUtil.getAreaFullname(address.getCityid()) + address.getAddress());
					}
					break;

				case SELECT_MARKET_CITY:
				/*
				 * marketProvince = (Province)
				 * Data.getSerializableExtra(ExtraName.province); marketCity =
				 * (City) Data.getSerializableExtra(ExtraName.city);
				 * selectMarket();
				 */
					break;
				case SELECT_MARKET:
					selectMarket = (Market) data.getSerializableExtra(ExtraName.market);
					mPifashichangEdit.setText(selectMarket.getName());
					break;
				case 100:
					mProductList.get(0).onActivityResult(requestCode, resultCode, data);
					break;
				case 101:
					mProductList.get(1).onActivityResult(requestCode, resultCode, data);
					break;
				case 102:
					mProductList.get(2).onActivityResult(requestCode, resultCode, data);
					break;
				case 103:
					mProductList.get(3).onActivityResult(requestCode, resultCode, data);
					break;
				case 104:
					mProductList.get(4).onActivityResult(requestCode, resultCode, data);
					break;
			}
		}
	}

	/**
	 * 确定对话框
	 */
	private void showSureDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("确认发布?");
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				release();
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
			}
		});
		builder.create().show();
	}

	/**
	 * 发布
	 */
	private void release() {

		String title = "";
		String bigpid = "";
		String pid = "";
		String catid = "";
		String varieties = "";
		String specifications = "";
		String num = "";
		String unit = "";
		boolean flag = false;
		for (BuyProduct buyProduct : mProductList) {
			if (buyProduct.check()) {
				flag = true;
				title += buyProduct.getSecond().getCatname() + ",";
				bigpid += buyProduct.getSecond().getBigpid() + ",";
				pid += buyProduct.getSecond().getCatid() + ",";
				catid += buyProduct.getThird().getCatid() + ",";
				varieties += buyProduct.getThird().getCatid() + ",";
				num += buyProduct.getNum() + ",";
				unit += buyProduct.getUnit() + ",";
				String guige = buyProduct.getGuige();
				specifications += guige + ",";
			}
		}
		if (!flag) {
			showToast("至少填写一个完整商品信息！");
			return;
		}

		title = title.substring(0, title.length() - 1);
		bigpid = bigpid.substring(0, bigpid.length() - 1);
		pid = pid.substring(0, pid.length() - 1);
		catid = catid.substring(0, catid.length() - 1);
		varieties = varieties.substring(0, varieties.length() - 1);
		num = num.substring(0, num.length() - 1);
		unit = unit.substring(0, unit.length() - 1);
		specifications = specifications.substring(0, specifications.length() - 1);
		if (address == null) {
			showToast("请选择收获地址");
			return;
		}

		PurchasePublished purchase = new PurchasePublished();
		purchase.setTitle(title);
		purchase.setBigpid(bigpid);
		purchase.setPid(pid);
		purchase.setCatid(catid);
		purchase.setVarieties(varieties);
		purchase.setSpecifications(specifications);
		purchase.setNum(num);
		purchase.setUnit(unit);
		purchase.setCityid(address.getCityid());
		purchase.setAddressId(address.getId());

		purchase.setContent(mBuyYaoqiuEdit.getText().toString().trim());
		purchase.setStart(sendDate);
		purchase.setEnd(wantDate);

		if (mIsRelease.isChecked()) {
			if (marketCity != null) {
				purchase.setMarketcityid(marketCity.getId());
				purchase.setMarketcitycode(marketCity.getId());
				purchase.setLng(marketCity.lng);
				purchase.setLat(marketCity.lat);
			}
			if (selectMarket != null) {
				purchase.setMarketid(selectMarket.getId());
			}
		}

		purchase.setPosition(mDangkouweizhiEdit.getText().toString().trim());
		purchase.setYearmoney(mNiancaigoueEdit.getText().toString().trim());

		startLoadingDialog();
		RequestService.getInstance().purchasePublishedMore(this, purchase, BaseEntity.class, new RequestListener() {

			@Override
			public void onSuccess(int requestCode, BaseEntity resultData) {
				dismissLoadingDialog();
				if (resultData.isOk()) {
					showToast("发布成功");
					toPurchaseHall();
					finish();
				} else {
					showToast(resultData.getMsg());
				}
			}

			@Override
			public void onFailed(int requestCode, Exception error, String msg) {
				dismissLoadingDialog();
				showToast(R.string.please_check_network);
			}
		});
	}

	protected void toPurchaseHall() {
		Intent intent = new Intent(this, PurchaseHallActivity.class);
		startActivity(intent);
	}

	@Override
	protected void onPause() {
		super.onPause();

	}

	@Override
	protected void onDestroy() {
		if (cascadingMenuPopWindow != null) {
			cascadingMenuPopWindow.dismiss();
		}
		if (cascadingMenuPopWindowMarket != null) {
			cascadingMenuPopWindowMarket.dismiss();
		}
		super.onDestroy();
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (isChecked) {
			showRelease();
		} else {
			hideRelease();
		}
	}

	/**
	 * 隐藏底部的发布模块
	 */
	private void hideRelease() {
		if (mReleaseView == null) {
			mReleaseView = (LinearLayout) findViewById(R.id.ll_release);
			mReleaseView.measure(0, 0);
			releaseHight = mReleaseView.getMeasuredHeight();
		}
		ValueAnimator animator = ValueAnimator.ofInt(releaseHight, 0);
		animator.addUpdateListener(new AnimatorUpdateListener() {
			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				mReleaseView.getLayoutParams().height = (int) animation.getAnimatedValue();
				mReleaseView.requestLayout();

			}
		});
		animator.setDuration(250);
		if (cascadingMenuPopWindowMarket != null) {
			cascadingMenuPopWindowMarket.dismiss();
		}
		animator.start();
	}

	/**
	 * 显示底部的 发布模块
	 */
	private void showRelease() {
		ValueAnimator animator = ValueAnimator.ofInt(0, releaseHight);
		animator.addUpdateListener(new AnimatorUpdateListener() {
			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				mReleaseView.getLayoutParams().height = (int) animation.getAnimatedValue();
				mReleaseView.requestLayout();
				scrollView.smoothScrollBy(0, releaseHight - (int) animation.getAnimatedValue());
			}
		});
		animator.setDuration(250);
		animator.start();
	}

	/**
	 * 添加 商品 part
	 */
	private void addView(View view) {
		if (mAddList == null)
			mAddList = (LinearLayout) findViewById(R.id.ll_add_list);
		final BuyProduct buyProduct = new BuyProduct(this);
		int index = mProductList.indexOf(view) + 1;
		buyProduct.setId(index + 100);
		LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		mAddList.addView(buyProduct, index, layoutParams);
		mProductList.add(index, buyProduct);
		for (int i = 0; i < mProductList.size(); i++) {
			BuyProduct product = mProductList.get(i);
			product.visibleDeleteView();
			product.goneAddView();
			if (i == 0 && mProductList.size() == 1)
				product.goneDeleteView();

			if (i != 4 && i == mProductList.size() - 1)
				product.visibleAddView();

			if (i % 2 != 0)
				product.setBackgroundColor(Color.parseColor("#faf9fb"));

		}
		buyProduct.setAddOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				addView(buyProduct);
			}
		});
		buyProduct.setDeleteOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				deleteView(buyProduct);
			}
		});
	}

	/**
	 * 删除 商品 part
	 *
	 * @param view
	 */
	private void deleteView(View view) {
		mAddList.removeView(view);
		mProductList.remove(view);
		for (int i = 0; i < mProductList.size(); i++) {
			BuyProduct product = mProductList.get(i);
			product.visibleDeleteView();
			product.goneAddView();
			if (i == 0 && mProductList.size() == 1)
				product.goneDeleteView();

			if (i != 4 && i == mProductList.size() - 1)
				product.visibleAddView();

			if (i % 2 != 0)
				product.setBackgroundColor(Color.parseColor("#faf9fb"));

		}
	}
}
